//
//  UIViewController+IGRouter.m
//  jingdongfang
//
//  Created by BOE on 2017/8/11.
//  Copyright © 2017年 ZhiYu Hao. All rights reserved.
//

#import "UIViewController+IGRouter.h"
#import "AppDelegate.h"
#import "NSObject+IGHookMethod.h"
#import "IGBaseViewController.h"

@implementation UIViewController (IGRouter)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(swiz_viewWillAppear:);
        [self ig_swizzlingInClass:[self class]
                 originalSelector:originalSelector
                 swizzledSelector:swizzledSelector];
        
        SEL originalSelector2 = @selector(viewWillDisappear:);
        SEL swizzledSelector2 = @selector(swiz_viewWillDisappear:);
        [self ig_swizzlingInClass:[self class]
                 originalSelector:originalSelector2
                 swizzledSelector:swizzledSelector2];
        
        SEL originalSelector3 = @selector(viewDidAppear:);
        SEL swizzledSelector3 = @selector(swiz_viewDidAppear:);
        [self ig_swizzlingInClass:[self class]
                 originalSelector:originalSelector3
                 swizzledSelector:swizzledSelector3];
        
        SEL originalSelector4 = @selector(viewDidDisappear:);
        SEL swizzledSelector4 = @selector(swiz_viewDidDisappear:);
        [self ig_swizzlingInClass:[self class]
                 originalSelector:originalSelector4
                 swizzledSelector:swizzledSelector4];
    });
}

+ (void)ig_pushViewController:(nullable UIViewController *)viewController
                     animated:(BOOL)animated {
    [[self ig_rootNavigationController] pushViewController:viewController
                                                  animated:animated];
}

+ (void)ig_pushViewControllerClass:(Class _Nonnull)vcClass
                          animated:(BOOL)animated {
    if (![vcClass isSubclassOfClass:[UIViewController class]]) {
        return;
    }
    if ([[self ig_rootNavigationController].topViewController isKindOfClass:vcClass]) {
        return;
    }
    [self ig_pushViewController:[vcClass new] animated:animated];
}

+ (void)ig_presentViewController:(nullable UIViewController *)viewControllerToPresent
                        animated:(BOOL)flag
                      completion:(void (^ __nullable)(void))completion {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self ig_rootNavigationController] presentViewController:viewControllerToPresent
                                                         animated:flag
                                                       completion:completion]; 
    });
}
+ (void)ig_popViewController:(Class _Nonnull)vcClass
{
    NSMutableArray *allVC = [NSMutableArray arrayWithArray:[self ig_rootNavigationController].viewControllers];
    __block BOOL hasFat = NO;
    __block UIViewController *viewC  = [[UIViewController alloc] init];
    [allVC enumerateObjectsUsingBlock:^(UIViewController *vc, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([vc isKindOfClass:vcClass])
        {
            viewC = vc;
            hasFat = YES;
            *stop = YES;
        }
    }];
    if (hasFat)
    {
        [[self ig_rootNavigationController] popToViewController:viewC animated:YES];
    }
}

+ (void)ig_delectViewController:(Class _Nonnull)vcClass {
    NSMutableArray *allVC = [NSMutableArray arrayWithArray:[self ig_rootNavigationController].viewControllers];
    [allVC enumerateObjectsUsingBlock:^(UIViewController *vc, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([vc isKindOfClass:vcClass]) {
            vc = nil;
            [allVC removeObjectAtIndex:idx];
            *stop = YES;
        }
    }];
    self.ig_rootNavigationController.viewControllers = allVC;
}

+ (nullable UINavigationController *)ig_rootNavigationController {
    return [kAPPDELEGATE rootNavigationController];
}

#pragma mark - Method Swizzling

- (void)swiz_viewWillAppear:(BOOL)animated
{
    [self inject_viewWillAppear:animated];
    [self swiz_viewWillAppear:animated];
}

- (void)swiz_viewWillDisappear:(BOOL)animated
{
    [self inject_viewWillDisappear:animated];
    [self swiz_viewWillDisappear:animated];
}

- (void)swiz_viewDidAppear:(BOOL)animated
{
    [self inject_viewDidAppear:animated];
    [self swiz_viewDidAppear:animated];
}

- (void)swiz_viewDidDisappear:(BOOL)animated
{
    [self inject_viewDidDisappear:animated];
    [self swiz_viewDidDisappear:animated];
}

#pragma mark - inject codes

- (void)inject_viewWillAppear:(BOOL)animated
{
    if ([self isKindOfClass:[IGBaseViewController class]]) {
//        [self.navigationController setNavigationBarHidden:YES animated:YES];
        IGBaseViewController *baseVC = (IGBaseViewController *)self;
        [self.navigationController setNavigationBarHidden:baseVC.isNavgationBarHidden animated:YES];
    } else {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

- (void)inject_viewWillDisappear:(BOOL)animated
{
    
}

- (void)inject_viewDidAppear:(BOOL)animated
{

}

- (void)inject_viewDidDisappear:(BOOL)animated
{
    
}

@end
